<template>
  <px-dialog v-model="dialogTableVisible" :title="title" :width="1400">
    <px-table v-loading="loading" :data="list" height="calc(100vh - 339px)" show-overflow-tooltip highlight-current-row border>
      <px-table-column label="发布年份" width="90" prop="releaseYear" />
      <px-table-column label="CO₂" prop="CO2" />
      <px-table-column label="CH₄" prop="CH4" />
      <px-table-column label="N₂O" prop="N2O" />
      <px-table-column label="HFCs" prop="HFCs" />
      <px-table-column label="PFCs" prop="PFCs" />
      <px-table-column label="SF₆" prop="SF6" />
      <px-table-column label="NF₃" prop="NF3" />
      <px-table-column label="CO₂e" prop="CO2e" />
      <px-table-column label="排放因子来源" prop="factorSource" />
      <px-table-column label="修改人" prop="updateByName" />
      <px-table-column label="修改时间" prop="updateTime" />
      <template #empty>
        <px-empty :image-size="150" />
      </template>
    </px-table>
    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      style="margin-bottom: -10px"
      :total="total"
      @pagination="queryTable"
    />
  </px-dialog>
</template>
<script setup lang="ts">
import { ref, onMounted } from "vue";
import { getemissionFactorHistory } from "@/api/doubleCarbonManage/carbonAccounting";

interface Props {
  name: string;
}

const props = withDefaults(defineProps<Props>(), {});
const dialogTableVisible = defineModel({ type: Boolean, default: false });
const title = "发布年份历史版本";

const loading = ref(false);
const queryParams = ref({
  pageNum: 1,
  pageSize: 20
});
const total = ref(0);
const list = ref([]);
onMounted(() => {
  queryTable();
});
async function queryTable() {
  const params = {
    name: props.name,
    ...queryParams.value
  };
  const res = await getemissionFactorHistory(params);
  if (!res) return;
  list.value = res.data;
  queryParams.value.pageNum = res.pageNum;
  queryParams.value.pageSize = res.pageSize;
  total.value = res.total;
  for (let i = 0; i < list.value.length; i++) {
    const item = list.value[i];
    item.CO2 = item.factorValueDTOS.find(e => e.gasType === "CO2")?.value;
    item.CH4 = item.factorValueDTOS.find(e => e.gasType === "CH4")?.value;
    item.N2O = item.factorValueDTOS.find(e => e.gasType === "N2O")?.value;
    item.HFCs = item.factorValueDTOS.find(e => e.gasType === "HFCs")?.value;
    item.PFCs = item.factorValueDTOS.find(e => e.gasType === "PFCs")?.value;
    item.SF6 = item.factorValueDTOS.find(e => e.gasType === "SF6")?.value;
    item.NF3 = item.factorValueDTOS.find(e => e.gasType === "NF3")?.value;
    item.CO2e = item.factorValueDTOS.find(e => e.gasType === "CO2e")?.value;
  }
}
</script>
